package com.ljwy.gun;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import androidx.appcompat.widget.AppCompatSpinner;

import com.ljwy.gun.BaseActivity;
import com.ljwy.gun.CpSpinnerAdapter;
import com.ljwy.gun.MainActivity;
import com.ljwy.gun.R;
import com.ljwy.gun.bean.AcInfo;
import com.ljwy.gun.bean.CpInfo;
import com.ljwy.gun.bean.PersonInfo;
import com.ljwy.gun.okgoHelper.GsonConvert;
import com.ljwy.gun.okgoHelper.HttpRequestHelper;
import com.ljwy.gun.okgoHelper.JsonCallback;
import com.ljwy.gun.utils.cache.ACacheHelper;
import com.lzy.okgo.model.Response;
import com.rscja.deviceapi.interfaces.ConnectionStatus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.ljwy.gun.Constant.GET_CP_LIST2;
import static com.ljwy.gun.Constant.GET_CP_LIST3;
import static com.ljwy.gun.Constant.GET_PERSON_LIST;
import static com.ljwy.gun.Constant.IP;


public class ChooseCpActivity extends BaseActivity implements AdapterView.OnItemSelectedListener {

    @BindView(R.id.choose_tv)
    TextView chooseTv;
    @BindView(R.id.cp_spinner)
    AppCompatSpinner cpSpinner;

    @BindView(R.id.cp_spinner2)
    AppCompatSpinner cpSpinner2;

    private CpInfo info;
    private AcInfo acInfo;
    private AcInfo.DataBean dataBean1;
    private CpInfo.DataBean dataBean2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choose_cp);
        ButterKnife.bind(this);
        getActivity();
    }

    private void getActivity() {
        HashMap<String, String> hashMap = new HashMap<>();
        HttpRequestHelper.getRequest(this, IP + GET_CP_LIST2, hashMap, false, new JsonCallback<String>() {
            @Override
            public void onSuccess(Response<String> response) {
                System.out.println(response.body());
                acInfo = GsonConvert.fromJson(response.body(), AcInfo.class);
                AcSpinnerAdapter adapter = new AcSpinnerAdapter(ChooseCpActivity.this, acInfo.getData());
                cpSpinner.setAdapter(adapter);
                cpSpinner.setOnItemSelectedListener(ChooseCpActivity.this);
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
//                info = new CpInfo();
//                List<CpInfo.DataBean> list = new ArrayList<>();
//                CpInfo.DataBean bean = new CpInfo.DataBean();
//                bean.setCp_id(Integer.valueOf(ACacheHelper.getInstance().getString("CPID","")));
//                bean.setCp_name(ACacheHelper.getInstance().getString("CPNAME",""));
//                list.add(bean);
//                info.setData(list);
//                CpSpinnerAdapter adapter = new CpSpinnerAdapter(ChooseCpActivity.this, list);
//                cpSpinner.setAdapter(adapter);
//                cpSpinner.setOnItemSelectedListener(ChooseCpActivity.this);
            }
        });
    }

    private void getCpLit() {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("activity_id",String.valueOf(dataBean1.getId()));
        HttpRequestHelper.getRequest(this, IP + GET_CP_LIST3, hashMap, false, new JsonCallback<String>() {
            @Override
            public void onSuccess(Response<String> response) {
                System.out.println(response.body());
                info = GsonConvert.fromJson(response.body(), CpInfo.class);
                CpSpinnerAdapter adapter = new CpSpinnerAdapter(ChooseCpActivity.this, info.getData());
                cpSpinner2.setAdapter(adapter);
                cpSpinner2.setOnItemSelectedListener(ChooseCpActivity.this);
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
//                info = new CpInfo();
//                List<CpInfo.DataBean> list = new ArrayList<>();
//                CpInfo.DataBean bean = new CpInfo.DataBean();
//                bean.setCp_id(Integer.valueOf(ACacheHelper.getInstance().getString("CPID","")));
//                bean.setCp_name(ACacheHelper.getInstance().getString("CPNAME",""));
//                list.add(bean);
//                info.setData(list);
//                CpSpinnerAdapter adapter = new CpSpinnerAdapter(ChooseCpActivity.this, list);
//                cpSpinner.setAdapter(adapter);
//                cpSpinner.setOnItemSelectedListener(ChooseCpActivity.this);
            }
        });
    }

    @OnClick(R.id.confirm_btn)
    public void onViewClicked() {
        ACacheHelper.getInstance().putString("CPID", String.valueOf(dataBean2.getId()));
        ACacheHelper.getInstance().putString("ACID", String.valueOf(dataBean1.getId()));
//        ACacheHelper.getInstance().putString("CPNAME", String.valueOf(dataBean2.getCp_name()));
        intent();
    }

    private void intent() {
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
        switch (adapterView.getId()) {
            case R.id.cp_spinner:
                 dataBean1 = acInfo.getData().get(i);
                chooseTv.setText(String.format("%s", "您选择的是：" +  dataBean1.getName()));
                getCpLit();


                break;
            case R.id.cp_spinner2:
                dataBean2 = info.getData().get(i);
                chooseTv.setText(String.format("%s %s", "您选择的是：" + dataBean2.getName(), dataBean1.getName()));
                break;
        }


    }

    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {

    }

}